package com.js.purs.service.impl;

import java.util.List;

import com.js.basic.api.RemoteBasicService;
import com.js.basic.api.model.CodeRules;
import com.js.purs.domain.PurProdBudget;
import com.js.purs.domain.PurProdInfo;
import com.js.purs.enums.TableEnum;
import com.js.purs.mapper.*;
import com.ruoyi.common.core.domain.vo.BasicCode;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.aspectj.weaver.loadtime.Aj;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.js.purs.domain.PurProdPlan;
import com.js.purs.service.IPurProdPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 采购计划.审批通过后生成招标项目Service业务层处理
 *
 * @author lzq
 * @date 2024-06-11
 */

@Service
public class PurProdPlanServiceImpl implements IPurProdPlanService {
    @Autowired
    private PurProdPlanMapper purProdPlanMapper;

    @Resource
    private PurProdBudgetMapper purProdBudgetMapper;

    @Resource
    private PurProdInfoMapper purProdInfoMapper;

    @Resource
    private PurProdPlanFileMapper purProdPlanFileMapper;

    @Resource
    private PurAppRecordsMapper purAppRecordsMapper;

    @Resource
    private RemoteBasicService remoteBasicService;

    @Autowired
    private RedissonClient redissonClient;


    /**
     * 查询采购计划.审批通过后生成招标项目
     *
     * @param planCode 采购计划.审批通过后生成招标项目主键
     * @return 采购计划.审批通过后生成招标项目
     */
    @Override
    public PurProdPlan selectPurProdPlanByPlanCode(String planCode) {
        return purProdPlanMapper.selectPurProdPlanByPlanCode(planCode);
    }

    /**
     * 查询采购计划.审批通过后生成招标项目列表
     *
     * @param purProdPlan 采购计划.审批通过后生成招标项目
     * @return 采购计划.审批通过后生成招标项目
     */
    @Override
    public List<PurProdPlan> selectPurProdPlanList(PurProdPlan purProdPlan) {
        return purProdPlanMapper.selectPurProdPlanList(purProdPlan);
    }

    /**
     * 新增采购计划.审批通过后生成招标项目
     *
     * @param purProdPlan 采购计划.审批通过后生成招标项目
     * @return 结果
     */
    @Override
    public int insertPurProdPlan(PurProdPlan purProdPlan) {

        return purProdPlanMapper.insertPurProdPlan(purProdPlan);
    }

    /**
     * 修改采购计划.审批通过后生成招标项目
     *
     * @param purProdPlan 采购计划.审批通过后生成招标项目
     * @return 结果
     */
    @Override
    public int updatePurProdPlan(PurProdPlan purProdPlan) {
        return purProdPlanMapper.updatePurProdPlan(purProdPlan);
    }

    /**
     * 批量删除采购计划.审批通过后生成招标项目
     *
     * @param planCodes 需要删除的采购计划.审批通过后生成招标项目主键
     * @return 结果
     */
    @Override
    public int deletePurProdPlanByPlanCodes(String[] planCodes) {
        return purProdPlanMapper.deletePurProdPlanByPlanCodes(planCodes);
    }

    /**
     * 删除采购计划.审批通过后生成招标项目信息
     *
     * @param planCode 采购计划.审批通过后生成招标项目主键
     * @return 结果
     */
    @Override
    public int deletePurProdPlanByPlanCode(String planCode) {
        return purProdPlanMapper.deletePurProdPlanByPlanCode(planCode);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult getPlanCode(String tableName) {
        RLock lock = redissonClient.getLock(tableName);
        if (!lock.tryLock()) {
            return AjaxResult.error("该表编码被占用");
        }

        try {
            CodeRules code = remoteBasicService.getCodeByTableName(tableName);
            BasicCode basicCode = DateUtils.getBasicCode(code.getDateRule(), code.getSerialNumber());
            if (code.getEncodeNow().contains(basicCode.getTime_format())) {
                long index = Long.parseLong(code.getEncodeNow().substring(code.getTableCode().length(), code.getEncodeNow().length())) + code.getStep();
                code.setEncodeNow(code.getTableCode() + index);
            } else {
                code.setEncodeNow(code.getTableCode() + basicCode.getTime_format() + basicCode.getIndex());
            }

            remoteBasicService.updateCodeNowByTableName(code);
            return AjaxResult.success(code);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


        return AjaxResult.error("失败");
    }

    @Override
    public List<PurProdPlan> selectPurProdPlanByFinish() {

        return purProdPlanMapper.selectPurProdPlanByFinish();
    }

    @Override
    public List<PurProdPlan> selectPurProdPlanByFinishNewBest() {
        return purProdPlanMapper.selectPurProdPlanByFinishNewBest();
    }

    @Override
    public int delPursByPlanCode(String planCode) {
        int index = 0;
        index = purProdPlanFileMapper.deletePurProdPlanFileByPlanCode(planCode);
        index = purAppRecordsMapper.deletePurAppRecordsByPlanCode(planCode);
        List<PurProdBudget> budgets = purProdBudgetMapper.selectPurProdBudgetByPlanCode(planCode);
        for (PurProdBudget budget : budgets) {
            index = purProdInfoMapper.deletePurProdInfoByBgId(budget.getBgId());
        }
        index = purProdBudgetMapper.deletePurProdBudgetByPlanCode(planCode);
        index = purProdPlanMapper.deletePurProdPlanByPlanCode(planCode);
        return index;
    }
}
